package com.wei.duowan.bbs.utils;

import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.annotation.Nullable;

import com.wei.duowan.bbs.App;

/**
 * 作者：赵若位
 * 时间：2019/10/11 21:03
 * 联系方式：1070138445
 * 功能：Android按键出现震动
 */
public class VibrateUtils
{
    private static VibrateUtils mUtils;
    private Vibrator mVibrator;


    private VibrateUtils()
    {
        mVibrator = (Vibrator) App.getContext().getSystemService(App.getContext().VIBRATOR_SERVICE);
    }


    public static VibrateUtils getInstance()
    {
        if (mUtils == null)
        {
            synchronized (VibrateUtils.class)
            {
                if (mUtils == null)
                {
                    mUtils = new VibrateUtils();
                }
            }
        }
        return mUtils;
    }


    public void vibrate(@Nullable long duration)
    {
        if (mVibrator != null)
        {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            {
                mVibrator.vibrate(VibrationEffect.createOneShot(duration,VibrationEffect.DEFAULT_AMPLITUDE));
            }else
            {
                mVibrator.vibrate(duration);
            }
        }
    }

}
